<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../../js/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/common.css" media="all">
    <script src="../../js/layui/layui.all.js"></script>
    <script src="../../js/token.js"></script>
    <script src="../../js/config.js"></script>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-form-item">
        <lable class="layui-form-label">菜单名称:</lable>
        <div class="layui-input-block">
            <input type="text" class="layui-input"
                   id="menuname" lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <lable class="layui-form-label">菜单地址:</lable>
        <div class="layui-input-block">
            <input type="text" class="layui-input"
                   id="menuurl" lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <lable class="layui-form-label">是否有子菜单:</lable>
        <div class="layui-input-block">
            <select class="layui-input" id="select" >
                <option value="true">是</option>
                <option value="false">否</option>
            </select>
        </div>
    </div>
    <div style="text-align: center;">
        <button class="layui-btn layuiadmin-btn-admin save">提交</button>
    </div>
</div>
</body>

<script>
    var $ = layui.$ //由于layer弹层依赖jQuery，所以可以直接得到
    var layer = layui.layer
    var tree = layui.tree;
    function getUrlSearch(name) {
        // 未传参，返回空
        if (!name) return null;
        // 查询参数：先通过search取值，如果取不到就通过hash来取
        var after = window.location.search;
        after = after.substr(1) || window.location.hash.split('?')[1];
        // 地址栏URL没有查询参数，返回空
        if (!after) return null;
        // 如果查询参数中没有"name"，返回空
        if (after.indexOf(name) === -1) return null;

        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
        // 当地址栏参数存在中文时，需要解码，不然会乱码
        var r = decodeURI(after).match(reg);
        // 如果url中"name"没有值，返回空
        if (!r) return null;

        return r[2];
    }
    window.onload = function () {
        $.ajax({
            url:HOST+'admin/permission/PermissionFindById',
            type:'post',
            dataType:'json',
            data:{
                id:getUrlSearch('id'),
            },
            success:function (data) {
                if (!isNull(data.data)){
                    $("#menuurl").val(data.data.permission_url);
                    $("#select").val(data.data.permission_end);
                    $("#menuname").val(data.data.permission_menu_name);
                }
            }
        })
    }
    $(".save").on('click',function () {
        if(getUrlSearch('id')==-1){
        $.ajax({
            url:HOST+'admin/permission/PermissionUpdate',
            type:'post',
            dataType:'json',
            data:{
                pid:getUrlSearch('text'),
                text:$("#menuname").val(),
                url:$("#menuurl").val(),
                end:$("#select").val(),
            },
            success:function (data) {
                if(data.code==200){
                    layer.msg('编辑成功',{
                        icon:1,
                        time:1000,
                        end:function () {
                            window.parent.location.reload();
                        }
                    })
                }else {
                    layer.msg('编辑失败', {
                        icon: 2,
                        time: 1000,
                        end: function () {
                            window.parent.location.reload();
                        }
                    })
                }
            }
        })
        }else{
            $.ajax({
                url:HOST+'admin/permission/PermissionUpdate',
                type:'post',
                dataType:'json',
                data:{
                    id:getUrlSearch('id'),
                    text:getUrlSearch('text'),
                    url:$("#menuurl").val(),
                    end:$("#select").val()
                }
                ,
                success:function (data) {
                    if(data.code==200){
                        layer.msg('编辑成功',{
                            icon:1,
                            time:1000,
                            end:function () {
                                window.parent.location.reload();
                            }
                        })
                    }else {
                        layer.msg('编辑失败', {
                            icon: 2,
                            time: 1000,
                            end: function () {
                                window.parent.location.reload();
                            }
                        })
                    }
                }
            })
        }
    })
</script>
</html>
